package com.tingyuge.test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tingyuge.entity.Order;
import com.tingyuge.entity.User;
import com.tingyuge.mapper.IOrderMapper;
import com.tingyuge.mapper.IUserMapper;
import com.tingyuge.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import javax.xml.transform.Source;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/6
 */
public class MybatisTest1 {

    private IUserMapper userMapper;
    private IOrderMapper orderMapper;
    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
         userMapper = sqlSession.getMapper(IUserMapper.class);
        orderMapper = sqlSession.getMapper(IOrderMapper.class);
    }

    // 测试一对一
    @Test
    public void test01() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IOrderMapper userMapper = sqlSession.getMapper(IOrderMapper.class);
        List<Order> orderAndUser = userMapper.findOrderAndUser();

        for (Order order : orderAndUser) {
            System.out.println(order);
        }

    }

    // 测试一对多
    @Test
    public void test02() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserMapper userMapper = sqlSession.getMapper(IUserMapper.class);
        List<User> orderAndUser = userMapper.findAll();

        for (User order : orderAndUser) {
            System.out.println("==");
            System.out.println(order);
//            System.out.println(order.getOrderList());
        }

    }

    // 测试多对多------还是一对多
    @Test
    public void test03() throws IOException {
        List<User> orderAndUser = userMapper.findAllUserAndRole();

        for (User order : orderAndUser) {
            System.out.println("==");
            System.out.println(order);
//            System.out.println(order.getOrderList());
        }

    }

    @Test
    public void addUser(){
        User user = new User();
        user.setUsername("tingyuge");
        user.setId(3);
        userMapper.addUser(user);
    }

    @Test
    public void updateUser(){
        User user = new User();
        user.setUsername("tingyuge222");
        user.setId(3);
        userMapper.updateUser(user);
    }

    @Test
    public void selectUserTest(){
        List<User> list = userMapper.selectUser();
        list.stream().forEach(System.out::println);
    }

    @Test
    public void orderFindAll(){
        List<Order> list = orderMapper.findAll();
        list.stream().forEach(System.out::println);
    }


    @Test
    public void userAnnoatationOneToMany(){
        List<User> list = userMapper.findAllByAnnotation();
        list.stream().forEach(System.out::println);
    }
    @Test
    public void userAndRoleAnnoatationOneToMany(){
        List<User> list = userMapper.findRoleAndUserByAnnotation();
        list.stream().forEach(System.out::println);
    }

    @Test
    public void findAllTest(){
        PageHelper.startPage(1, 2);
        List<User> list = userMapper.findAll();
        list.stream().forEach(System.out::println);
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        System.out.println(userPageInfo.getTotal());
        System.out.println(userPageInfo.getPages());
    }

    @Test
    public void findAllTestMapper() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user  = new User();
        user.setId(1);
        User user2 = userMapper.selectOne(user);
        System.out.println(user2);

    }
}
